package com.showde.shoppingmall.activity;

import java.util.ArrayList;
import java.util.List;

import com.showde.shoppingmall.R;
import com.showde.shoppingmall.adapter.ProductCollectionAdapter;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.ProductCollectionLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.db.ProductCollectionDBCommand;
import com.showde.shoppingmall.domain.ProductCollection;
import com.showde.shoppingmall.utils.ToastUtils;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

public class MyCollectionActivity extends BaseActivity {

	private static final String TAG = "MyCollectionActivity";

	private static final int MSG_REFRESHDATA = 0x10001;

	private static final int MSG_REFRESHDATA_FAIL = 0x10002;

	private ListView myCollectionProduct;

	private ProductCollectionAdapter productCollectionAdapter;

	private ProductCollectionDBCommand collectionDBCommand;

	private List<ProductCollection> productCollections;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_REFRESHDATA:
				refreshCollectionList();
				break;
			case MSG_REFRESHDATA_FAIL:
				ToastUtils.showToast(MyCollectionActivity.this, "刷新收藏列表失败");
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public void onClick(View v) {

	}

	@Override
	protected void initParam() {
		if (collectionDBCommand == null) {
			collectionDBCommand = new ProductCollectionDBCommand(this);
		}
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_mycollection);
		myCollectionProduct = (ListView) findViewById(R.id.lst_collection);
	}

	@Override
	protected void onResume() {
		super.onResume();
		fetchColletionProduct();
	}

	private void fetchColletionProduct() {
		ProductCollectionLogic.getCollectionProduct(this,
				new UICallbackInterface() {

					@Override
					public void onSuccess(CallbackData data) {
						if (data != null) {
							handler.sendEmptyMessage(MSG_REFRESHDATA);
						}
					}

					@Override
					public void onError(String errorMsg) {
						handler.sendEmptyMessage(MSG_REFRESHDATA_FAIL);
					}
				});
	}

	private void refreshCollectionList() {

		productCollections = collectionDBCommand.queryAll();// collectionDBCommand.queryAllSimple();
		// if (productCollections == null || productCollections.size() == 0) {
		// productCollections = collectionDBCommand.queryAll();
		// }
		if (productCollections == null) {
			productCollections = new ArrayList<ProductCollection>();
		}
		productCollectionAdapter = (ProductCollectionAdapter) myCollectionProduct
				.getAdapter();
		if (productCollectionAdapter == null) {
			productCollectionAdapter = new ProductCollectionAdapter(this,
					productCollections);
			myCollectionProduct.setAdapter(productCollectionAdapter);
		} else {
			productCollectionAdapter.resetData(productCollections);
		}
		productCollectionAdapter.notifyDataSetChanged();
	}

}
